class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return 0
        if len(nums) <=3:
            return max(nums)
        
        dp = [[0 for _ in range(len(nums))] for _ in range(len(nums))]
        for i in range(len(nums)):
            dp[i][i] = nums[i]

        globalMaxValue = 0
        for stride in range(1, len(nums)-1):
            for begin in range(0, len(nums)):
                end = begin + stride
                maxValue = 0
                for i in range(begin, end+1):
                    firstRange = (begin, i-2)
                    secondRange = (i+2, end)
                    value = nums[i%len(nums)]
                    if firstRange[0] <= firstRange[1]:
                        value += dp[firstRange[0]%len(nums)][firstRange[1]%len(nums)]
                    if secondRange[0] <= secondRange[1]:
                        value += dp[secondRange[0]%len(nums)][secondRange[1]%len(nums)]
                    maxValue = max(maxValue, value)
                dp[begin%len(nums)][end%len(nums)] = maxValue
                globalMaxValue = max(globalMaxValue, maxValue)
        return globalMaxValue

s = Solution()
print(s.rob([1,1,1,1]))

